<?php

class yougoGuardValidatorUser extends sfValidatorBase {

    protected function doClean($values) {
        if ($values['email']) {
            try {
                YougoRestClient::init($values['email'], $values['password']);
                $user = UserRestResource::singleton()->find($values['email']);
            } catch (Exception $e) {
                throw new sfValidatorErrorSchema($this, array('email' => new sfValidatorError($this, 'invalid')));
            }

            if ($user && $user->active)
                return array_merge($values, array('user' => $user, 'basicAuth' => base64_encode($values['email'].':'.$values['password'])));

            throw new sfValidatorErrorSchema($this, array('email' => new sfValidatorError($this, 'invalid')));
        }
    }

}
